﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace WebSocketService.Tools
{
    public class UserInfoHelper
    {
        /// <summary>
        /// 个人信息key
        /// </summary>
        private const string _userKey = "UserMsg";

        /// <summary>
        /// 保存用户与链接的对应关系
        /// </summary>
        public static bool AddConnectionId(MessageModel model, Guid connectionId)
        {
            var cache = CacheHelper.GetCache(_userKey);
            if (cache.IsNotNull())
            {
                var models = cache.ToJson().DeserializeJsonToList<UserModel>();
                if (models.IsNotNull())
                {
                    models = models.Where(m => m.PassDateTime > DateTime.Now).ToList();//过滤掉过期的
                    var res = models.Any(m => m.Token == model.Token && m.ConnectionId == connectionId);
                    if (!res)
                    {
                        var t = new UserModel
                        {
                            Token = model.Token,
                            ConnectionId = connectionId,
                            ModelCode = model.ModelCode,
                            PassDateTime = DateTime.Now.AddDays(1)
                        };
                        models.Add(t);
                        CacheHelper.SetCache(_userKey, models);
                    }
                }
            }
            else
            {
                cache = new List<UserModel>
                {
                    new UserModel
                    {
                        Token = model.Token,
                        ConnectionId = connectionId,
                        ModelCode = model.ModelCode,
                        PassDateTime = DateTime.Now.AddDays(1)
                    }
                };
                CacheHelper.SetCache(_userKey, cache);
            }
            return true;
        }

        /// <summary>
        /// 获取用户对应的链接信息
        /// </summary>
        /// <param name="tokens">用户令牌</param>
        /// <returns></returns>
        public static List<UserModel> GetUserConnections(List<string> tokens)
        {
            var result = new List<UserModel>();
            var cache = CacheHelper.GetCache(_userKey);
            if (cache.IsNotNull())
            {
                var models = cache.ToJson().DeserializeJsonToList<UserModel>();
                if (models.IsNotNull())
                {
                    models = models.Where(m => m.PassDateTime > DateTime.Now).ToList();//过滤掉过期的数据
                    result = models.Where(m => tokens.Contains(m.Token)).ToList();
                    CacheHelper.SetCache(_userKey, models);
                }

            }

            return result;
        }

        /// <summary>
        /// 获取用户对应的链接信息
        /// </summary>
        /// <param name="modelCode">模块编码</param>
        /// <returns></returns>
        public static List<UserModel> GetModelConnections(string modelCode)
        {
            var result = new List<UserModel>();
            var cache = CacheHelper.GetCache(_userKey);
            if (cache.IsNotNull())
            {
                var models = cache.ToJson().DeserializeJsonToList<UserModel>();
                if (models.IsNotNull())
                {
                    models = models.Where(m => m.PassDateTime > DateTime.Now).ToList();//过滤掉过期的数据
                    result = models.Where(m => m.ModelCode == modelCode).ToList();
                    CacheHelper.SetCache(_userKey, models);
                }

            }

            return result;
        }

    }


    /// <summary>
    /// UserModel数据实体
    /// </summary>
    public class UserModel
    {
        /// <summary>
        /// 用户令牌
        /// </summary>
        public string Token { set; get; }

        /// <summary>
        /// 链接标识
        /// </summary>
        public Guid ConnectionId { set; get; }

        /// <summary>
        /// 模块编码
        /// </summary>
        public string ModelCode { set; get; }

        /// <summary>
        /// 过期时间
        /// </summary>
        public DateTime PassDateTime { set; get; }
    }
}